import { defineStore } from 'pinia'
import { reactive, shallowRef } from 'vue'
import { RouteRecordRaw } from "vue-router";
import { routes } from "@/router/routes.ts";
import { FormModel, MediaVerifyType, RealNameStatus, Sex, UserInfo } from "@/api/user/type.ts";
import { getUserInfoReq, loginReq, registerReq, updateAvatarReq, updateIntroductionReq, updateUsernameReq } from "@/api/user/user.ts";

export const useUserStore = defineStore('user', () => {
    return {
        userState: reactive<UserState>({}),
        userInfo: reactive<UserInfo>({
            id: null,
            username: '',
            phone: '',
            email: '',
            sex: Sex.Unknown,
            introduction: '',
            nickname: '',
            avatar: '',
            mediaVerifyType: MediaVerifyType.Unverified,
            realNameStatus: RealNameStatus.Unverified,
            createdTime: ''
        }),
        routes: shallowRef<RouteRecordRaw[]>(routes),
        setToken(token: string) {
            this.userState.token = token
        },
        /**
         * 登录
         */
        async login(data: FormModel) {
            const res = await loginReq(data);
            if (res.code === 200 && res.data && res.data.token) {
                this.userState.token = res.data.token
                this.userState.username = res.data.username
                return Promise.resolve(res)
            } else {
                this.userState.token = ''
                return Promise.reject(res)
            }
        },
        /**
         * 注册
         */
        async register(data: FormModel) {
            const res = await registerReq(data);
            if (res.code === 200 && res.data && res.data.token) {
                this.userState.token = res.data.token
                this.userState.username = res.data.username
                return Promise.resolve(res)
            } else {
                this.userState.token = ''
                return Promise.reject(res)
            }
        },

        async getUserInfo() {
            const res = await getUserInfoReq()
            if (res.code === 200 && res.data) {
                this.userInfo = res.data
                return Promise.resolve(res)
            } else {
                return Promise.reject(res)
            }
        },

        /**
         * 更新头像
         */
        async updateAvatar(file: File) {
            const res = await updateAvatarReq(file)
            if (res.code === 200 && res.data) {
                this.userInfo.avatar = res.data
                return Promise.resolve(res)
            } else {
                return Promise.reject(res)
            }
        },

        /**
         * 更新用户名
         */
        async updateUserName(username: string) {
            const res = await updateUsernameReq(username)
            if (res.code === 200) {
                return Promise.resolve(res)
            } else {
                return Promise.reject(res)
            }
        },
        /**
         * 更新用户自我介绍
         */
        async updateIntroduction(introduction: string) {
            const res = await updateIntroductionReq(introduction)
            if (res.code === 200) {
                return Promise.resolve(res)
            } else {
                return Promise.reject(res)
            }
        }
    }
}, {
    persist: {
        paths: ['userState', 'userInfo']
    }
})
